<?php 
/**
 *  后台产品相关操作
 */
class ProductsController extends BaseController{
   
    /**
     * 产品录入页面显示
     */
    public function showAddProduct() {
        $pid = empty($_GET['pid'])?NULL: $this->getParamInt('pid' , true , true );
        if( $pid ){
            $productInfo = Product_Info::getInstance()->getProuctInfoByPId( $pid );
            //查找小类
            $productSCat = Category_Show::getInstance()->_getCategory( $productInfo['SCat'] );
            //查找品牌
            $productBrand = Brand_Search::getInstance()->getBrandInfo( $productInfo['PBrand']);
            //查找消费水平
            $arrLevel = Product_Levels::getInstance()->productLevel( $pid );
            //查找年龄段
            $arrAge = Product_Age::getInstance()->productAge( $pid );
            //查找性别
            $arrSex = Product_Sex::getInstance()->productSex( $pid );
        }
        /**
         * 产品大类型显示
         */
        $bcat = Category_Show::getInstance()->getCategory(0);
        /**
         * 消费层次
         */
        $levels = Product_Info::getInstance()->getLevels();
        /**
         * 适用年龄段
         */
        $age = Product_Info::getInstance()->getAge();
        /**
         * 所有性别
         */
        $sex = Product_Info::getInstance()->getSex();
        include TPL_DIR . 'addProduct.php';
    }
    
    /**
     * 产品录入操作
     */
    public function addProduct() {

        $PName = $this->getParamString('goods_name', true, true);
        $BType = $this->getParamInt('bcat_id');
        $Stype = $this->getParamInt('scat_id');
        $Brand = $this->getParamInt('brand_id');
        $Price = $this->getParamInt('shop_price');
        
        /**
         * 相关维度Id
         */
        $level = $this->getParamIntArray('level');
        $age = $this->getParamIntArray('age');
        $sex = $this->getParamIntArray('sex');

       
        /**
         * 修改产品
         */
        $pid = empty($_POST['pid'])?NULL:$this->getParamInt('pid');
        if( $pid ){
            
            $PicThumb = $this->getParamString( 'PicGoods' , true ,true );
            $PicSource = $this->getParamString( 'PicOriginal' , true , true );
            
           /**
            * 修改produt_info 表
            */
            Product_Info::getInstance()->updateProduct( $pid ,$PName, $BType, $Stype, $Brand, $Price, $PicThumb, $PicSource);
            /**
             * 修改product_levels 表
             */
            Product_Levels::getInstance()->delProduct( $pid );
            Product_Levels::getInstance()->addProductLevels( $pid ,$level );
            /**
             * 修改 product_age 表 
             */
            Product_Age::getInstance()->delProduct( $pid);
            Product_Age::getInstance()->addProductAge( $pid ,$age );
            /**
             * 修改 product_sex类
             */
            Product_Sex::getInstance()->delProduct( $pid);
            Product_Sex::getInstance()->addProductSex( $pid ,$sex );
            
        }
        else{
            
             //获取信息
        $bgImgThumb = $_FILES['goods_thumb'];
        $bgImgSource = $_FILES['goods_img'];

        if ($bgImgThumb['error'] !== 0 || !is_uploaded_file($bgImgThumb['tmp_name']) || $bgImgSource['error'] !== 0 || !is_uploaded_file($bgImgSource['tmp_name'])) {
            throw new AppException(2003);
        }
        $path_thumb = "file/uploads/goods_img";                            //缩略图图片
        $path_source = "file/uploads/source_img";                          //源图


        /**
         * 上传图片
         */
        if( !empty( $bgImgThumb) || !empty( $bgImgSource )){
             $PicThumb = Product_Info::getInstance()->imgUpload($bgImgThumb, $path_thumb ,100 , 100 );
             $PicSource = Product_Info::getInstance()->imgUpload($bgImgSource, $path_source , 350 , 350);
        }
       
         /**
         * 添加到product_info 表
         */
       Product_Info::getInstance()->addProductToSql( $PName, $BType, $Stype, $Brand, $Price, $PicThumb, $PicSource);
       $pid = mysql_insert_id();
       
        /**
         * 添加到 product_levels 
         */
        Product_Levels::getInstance()->addProductLevels($pid, $level);
        /**
         * 添加到 produt_age
         */
        Product_Age::getInstance()->addProductAge($pid, $age);
        /**
         * 添加到 product_Sex
         */
        Product_Sex::getInstance()->addProductSex($pid, $sex);  
            
        }
  

        /**
         * 根据产品的维度筛选用户推荐产品
         */
        $push = empty($_POST['push']) ? NULL : $this->getParamInt('push');
        if ($push) {
            
            $dimensions = array();

            if (!empty($Brand)) {
                $dimensions['PBrand'] = $Brand;
            }
            if (!empty($BType)) {
                $dimensions['BCat'] = $BType;
            }
            if (!empty($SType)) {
                $dimensions['SCat'] = $SType;
            }
            
            if ( !empty ($level) ) {
                foreach ($level as $value) {
                    if ( $value == 5 ) {
                        $dimensions['DHihgLevels'] = 1;
                    }
                    if ( $value == 4 ) {
                        $dimensions['DMiddleLevels'] = 1;
                    }
                    if ( $value == 3 ) {
                        $dimensions['DLowLevels'] = 1;
                    }
                }
            }
            if ( !empty ($age) ) {
                foreach ($age as $value) {
                    if ( $value == 6 ) {
                        $dimensions['DOldAge'] = 1;
                    }
                    if ( $value == 7 ) {
                        $dimensions['DYouthAge'] = 1;
                    }
                    if ( $value == 8 ) {
                        $dimensions['DInfancyAge'] = 1;
                    }
                }
            }
            if ( !empty ($sex) ) {
                foreach ($sex as $value) {
                    if ( $value == 1 ) {
                        $dimensions['DManSex'] = 1;
                    }
                    if ( $value == 2 ) {
                        $dimensions['DWomanSex'] = 1;
                    }
                }
            }
             $userID = Backstage_UserInfo::getInstance()->getScreening( $dimensions);
             if( $userId)
             /**
             * 推送产品
             */
             $userInfo = Backstage_UserInfo::getInstance()->getUserInfo( $userID ,$pid ); 
        }
        include TPL_DIR . 'index.php';
    }

    
     /**
     * 产品删除
     */
    public function deleteProduct(){
        $pid  = $this->getParamInt( 'pid' , true , true );
        //删除产品表
        $productInfo = Product_Info::getInstance()->delProduct( $pid );
        //删除product_level 表
        $productLevel = Product_Levels::getInstance()->delProduct( $pid );
        //删除product_age 表
        $productAge = Product_Age::getInstance()->delProduct( $pid );
        //删除product_sex表
        $productSex = Product_Sex::getInstance()->delProduct( $pid );
    }  
     /**
     * 根据产品大类获得产品小类
     */  
    public function getSCat() {
        $TId = $_GET['TId'];
        $SCat = Category_Show::getInstance()->getCategory($TId);
        $option = '<option value=0 >'.请选择.'</option>';

        foreach ($SCat as $key => $value) {
            $option .='<option value="' . $value["TId"].'">' . $value['TName'] . '</option>';
        }
        echo  $option;
    }

    /**
     * 根据产品类型查询品牌
     */
    public function getBrand() {
        $TId = $_GET['TId'];
        $Brand = Brand_Search::getInstance()->getProductBrand($TId);
        //return $Brand;
         $option = '<option value=0 >'.请选择.'</option>';
        foreach ($Brand as $key => $value) {
            $option .='<option value="' . $value["BId"] . '">' . $value['BName'] . '</option>';
        }
        echo $option;
    }
       /**
     * 获得所有产品列表
     */
    public function showProduct(){
        //产品大类
        $bcat = Category_Show::getInstance()->getCategory(0);
          /**
         * 消费层次
         */
        $levels = Product_Info::getInstance()->getLevels();
        /**
         * 适用年龄段
         */
        $age = Product_Info::getInstance()->getAge();
        /**
         * 所有性别
         */
        $sex = Product_Info::getInstance()->getSex();
        
        $produtInfo = Product_Info::getInstance()->getAllProductInfo();
        
        foreach( $produtInfo as $key => $value){
            $bcat = Category_Show::getInstance()->getCategory( 0 );
           
            $array[$value['PId']]= array(
            'productName' => $value['PName'],
            'BCat' => Category_SHow::getInstance()->getCat( $value['BCat'] ),
            'SCat' => Category_SHow::getInstance()->getCat( $value['SCat'] ),
            'Brand'=> Brand_Search::getInstance()->getBrand( $value['PBrand'] ),
            'Price'=> $value['Price']
               );   
        }
        
        include TPL_DIR.'listProduct.php';
        
        
    }
    
    /**
     * 搜索维度推荐产品
     */
    public function PushProdut(){
        $BType = empty( $_POST['bcat_id'])?NULL: $this->getParamInt('bcat_id');
        $Stype = empty( $_POST['scat_id'])?NULL: $this->getParamInt('scat_id');
        $Brand = empty( $_POST['brand_id'])?NULL:$this->getParamInt('brand_id');

        /**
         * 相关维度Id
         */
        $level = empty( $_POST['level'])?NULL: $this->getParamIntArray('level');
        $age   = empty( $_POST['age'])?NULL:$this->getParamIntArray('age');
        $sex   = empty( $_POST['sex'])?NULL:$this->getParamIntArray('sex');
        $dimensions = array();
        if(!empty( $BType)){
             $dimensions['BCat'] = $BType;
        }
        if( !empty ($Stype )){
            $dimensions['SCat'] = $Stype;
        }
        if( !empty($Brand )){
            $dimensions['PBrand'] =$Brand;
            
        }
        if(!empty( $level)){
            foreach( $level as $key => $value){
                   if ( $value == 5 ) {
                        $dimensions['DHihgLevels'] = 1;
                    }
                    if ( $value == 4 ) {
                        $dimensions['DMiddleLevels'] = 1;
                    }
                    if ( $value == 3 ) {
                        $dimensions['DLowLevels'] = 1;
                    }
                
            }
            
        }
        if( !empty( $age )){
            foreach( $age as $key => $value){
               if ( $value == 6 ) {
                        $dimensions['DOldAge'] = 1;
                    }
                    if ( $value == 7 ) {
                        $dimensions['DYouthAge'] = 1;
                    }
                    if ( $value == 8 ) {
                        $dimensions['DInfancyAge'] = 1;
                    } 
            }
            
        }
         if ( !empty ($sex) ) {
                foreach ($sex as $value) {
                    if ( $value == 1 ) {
                        $dimensions['DManSex'] = 1;
                    }
                    if ( $value == 2 ) {
                        $dimensions['DWomanSex'] = 1;
                    }
                }
            }
       //获取产品的id
       $productArr = Product_push::getInstance()->getPushInfo( $dimensions );
      //获取用户的id
       if( is_array($productArr )){
           $userUId = Backstage_UserInfo::getInstance()->getSuccessUser( $dimensions );
           if( is_array( $userUId )){
                $userId = implode(',',$userUId);
           }
           else{
               $userId = NULL;
           }
          
           
       }
       //  $userSearchInfo = User_Info::getUserInfoByUId( array( '0' =>1));
        include TPL_DIR.'result.php'; 
    }
    /**
     *  发送邮箱
     */
    public function pushProducts(){
         $pid = $this->getParamInt( 'pid' , true ,true );
         if( empty($_GET['uid'])){
             echo '没有符合该产品的用户';
             return false;
             
         }
         $userID = explode(',' , $_GET['uid'] ) ; 
         
         $userInfo = Backstage_UserInfo::getInstance()->getUserInfo( $userID ,$pid );
         if( $userInfo){
              echo '<script type="text/javascript">alert("发送成功")</script>';
              Helper_Url::redirect('admin.php?con=Products&act=showProduct');
         }
         
    }   
    
}